#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2011 OpenFOAM Foundation
#     Copyright (C) 2016-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     etc/relocateQt
#
# Description
#     Change prefix when relocating QT installation
#
# ----------------------------------------------
# NO USER-CONFIGURABLE SETTINGS WITHIN THIS FILE
#------------------------------------------------------------------------------
# Run from third-party directory only
wmakeCheckPwd "$WM_THIRD_PARTY_DIR" 2>/dev/null || {
    echo "Error (${0##*/}) : current directory is not \$WM_THIRD_PARTY_DIR"
    echo "    Check your OpenFOAM environment and installation"
    exit 1
}
. etc/tools/ThirdPartyFunctions
. etc/tools/QtFunctions
#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

usage: ${0##*/} [OPTION] [qt-VERSION]
options:
  -force            Create qt.conf and edit pkgconfig to use \${prefix}
  -help

Adjust prefix in qt.conf and pkgconfig files after relocating third-party
with a QT installation.

* adjust relocation prefix for $qtVERSION

USAGE
    exit 1
}
#------------------------------------------------------------------------------
unset optForce

# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    '') ;;      # Ignore empty
    -h | -help*) usage   ;;

    -f | -force)
        optForce=true
        ;;
    -qt-[0-9]* | [0-9]* | qt-*)
        # -qt-VERSION, VERSION, qt-VERSION, qt-everywhere-opensource-src-VERSION
        qtVERSION="${1%%/}";
        qtVERSION="${qtVERSION##*-}"
        ;;
    *)
        die "unknown option/argument: '$1'"
        ;;
    esac
    shift
done

#------------------------------------------------------------------------------
QT_ARCH_PATH=$installBASE/qt-$qtVERSION

[ -n "$qtVERSION" ]    || die "No QT version specified"
[ -d "$QT_ARCH_PATH" ] || die "No QT installation"

if [ "${optForce:-false}" = true ]
then
    # Create qt.conf and adjust locations to use '${prefix}' internally
    finalizeQt
fi
pkgconfigNewPrefix "$QT_ARCH_PATH"

# -----------------------------------------------------------------------------
